#include "TextureLoader.h"
#include <SDL_image.h>
#include "Game.h"

#define TILE_SIZE 34
#define BULLET_TEXTURE_SIZE 9
#define EnemyTextureSize 34

#define EXPLOSION_TEXTURE_START_X 20
#define EXPLOSION_TEXTURE_START_Y 4

TextureLoader &TextureLoader::Instance()
{
    static TextureLoader instance;
    return instance;
}

TextureLoader::~TextureLoader()
{
    for(auto texture : terrainTextures_)
    {
        if(texture)
        {
            SDL_DestroyTexture(texture);
        }
    }
    SDL_DestroyTexture(source_);
}

Texture TextureLoader::GetEnemyTexture(Angle angle, EnemyType type, bool index, bool islight)
{
    SDL_Rect srcRect = {TILE_SIZE*((type << 3) + (angle << 1) + (index? 0:1)), (2+(islight? 1:0))*TILE_SIZE, TILE_SIZE, TILE_SIZE};
    return Texture(source_,srcRect);
}

Texture TextureLoader::GetPlayer1Texture(Angle angle,int level, bool index)
{
    SDL_Rect srcRect = {TILE_SIZE*((level << 3) + (angle << 1) + (index? 0:1)), 0, TILE_SIZE, TILE_SIZE};
    return Texture(source_,srcRect);
}

Texture TextureLoader::GetPlayer2Texture(Angle angle,int level, bool index)
{
    SDL_Rect srcRect = {TILE_SIZE*((level << 3) + (angle << 1) + (index? 0:1)), TILE_SIZE, TILE_SIZE, TILE_SIZE};
    return Texture(source_, srcRect);
}

Texture TextureLoader::GetBulletTexture(Angle angle)
{
    return Texture(source_, bulletSrcRects_[angle]);
}

SDL_Texture *TextureLoader::GetTerrainTexture(TerrainType type) const
{
    return terrainTextures_[type];
}

Texture TextureLoader::GetExplosionTexture(uint32_t current_frame) const
{
    return Texture(source_,explosionTextures_[current_frame]);
}

Texture TextureLoader::GetGenEnemyAnimationTexture(uint32_t current_frame)
{
    SDL_Rect srcRect = {(16+current_frame)*TILE_SIZE,4*TILE_SIZE,TILE_SIZE,TILE_SIZE};
    return Texture(source_, srcRect);
}

TextureLoader::TextureLoader()
{
    auto renderer_ = Game::GetInstance().GetRenderer();
    source_ = IMG_LoadTexture(Game::GetInstance().GetRenderer(), "fodder/pic/tank_sprite.png");
    bulletSrcRects_[Angle::UP]      = {0, 5*TILE_SIZE, BULLET_TEXTURE_SIZE, BULLET_TEXTURE_SIZE};
    bulletSrcRects_[Angle::DOWN]    = {BULLET_TEXTURE_SIZE, 5*TILE_SIZE, BULLET_TEXTURE_SIZE, BULLET_TEXTURE_SIZE};
    bulletSrcRects_[Angle::LEFT]    = {0, 5*TILE_SIZE+BULLET_TEXTURE_SIZE, BULLET_TEXTURE_SIZE, BULLET_TEXTURE_SIZE};
    bulletSrcRects_[Angle::RIGHT]   = {BULLET_TEXTURE_SIZE, 5*TILE_SIZE+BULLET_TEXTURE_SIZE, BULLET_TEXTURE_SIZE, BULLET_TEXTURE_SIZE};
    //加载地形贴图资源
    terrainTextures_[TerrainType::Empty]    = nullptr;
    terrainTextures_[TerrainType::Brick]    = IMG_LoadTexture(renderer_, "fodder/pic/Brick.png");
    terrainTextures_[TerrainType::ICE]      = IMG_LoadTexture(renderer_, "fodder/pic/Ice.png");
    terrainTextures_[TerrainType::Water]    = IMG_LoadTexture(renderer_, "fodder/pic/Water.png");
    terrainTextures_[TerrainType::Iron]     = IMG_LoadTexture(renderer_, "fodder/pic/Iron.png");
    terrainTextures_[TerrainType::Junjle]   = IMG_LoadTexture(renderer_, "fodder/pic/Junjle.png");

    explosionTextures_[0] = {TILE_SIZE*EXPLOSION_TEXTURE_START_X,TILE_SIZE*EXPLOSION_TEXTURE_START_Y,TILE_SIZE,TILE_SIZE};
    explosionTextures_[1] = {TILE_SIZE*(EXPLOSION_TEXTURE_START_X+1),TILE_SIZE*EXPLOSION_TEXTURE_START_Y,TILE_SIZE,TILE_SIZE};
    explosionTextures_[2] = {TILE_SIZE*(EXPLOSION_TEXTURE_START_X+2),TILE_SIZE*EXPLOSION_TEXTURE_START_Y,TILE_SIZE,TILE_SIZE};
    explosionTextures_[3] = {TILE_SIZE*(EXPLOSION_TEXTURE_START_X+3),TILE_SIZE*EXPLOSION_TEXTURE_START_Y,TILE_SIZE*2,TILE_SIZE*2};
    explosionTextures_[4] = {TILE_SIZE*(EXPLOSION_TEXTURE_START_X+5),TILE_SIZE*EXPLOSION_TEXTURE_START_Y,TILE_SIZE*2,TILE_SIZE*2};
}
